package com.example.demo;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class BinaryCount {

    /**
     * 把一个数转为二进制，求里面1的个数，此题错误
     */
    @Test
    public void binaryCount() {
        int a = hammingWeight(4L);
    }

    public int hammingWeight(long n) {
        int num = 0;
        List<Long> list = new ArrayList<>();

        while (n > 0) {
            long b = n % 2;
            list.add(b);
            n = n / 2;
        }

        System.out.println(list);

        for (Long j : list) {
            if (j == 1) {
                num++;
            }
        }
        return num;
    }
}
